package com.lovely3x.jobservice.executors.extensions.downloader2.executor;

import android.os.Parcel;
import android.os.Parcelable;

import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.lovely3x.jobservice.task.Progress;

/**
 * 块描述一个下载的块
 */
@Table(DownloadTaskExecutor.TAB_PARTS)
public class Part implements Parcelable {

    @PrimaryKey(AssignType.AUTO_INCREMENT)
    @Column("_id")
    public int id;


    /**
     * 这个块的的状态
     */
    @Column("_state")
    public int state = Progress.State.IDLE;

    /**
     * 任务的what
     */
    @Column("_what")
    public long taskWhat;

    /**
     * 任务的下载地址
     */
    @Column("_url")
    public String url;

    /**
     * 下载任务的文件保存
     */
    @Column("_file")
    public String file;

    /**
     * 开始下载位置
     */
    @Column("_start")
    public long start;

    /**
     * 文件已经下载的位置
     */
    @Column("_current")
    public long current;

    /**
     * 文件下载的结束位置
     */
    @Column("_end")
    public long end;

    /**
     * 这个块的编号
     */
    @Column("_sequence")
    public int sequence;

    Part() {
    }

    Part(int state, long taskWhat, String url, String file, long start, long current, long end, int sequence) {
        this.state = state;
        this.taskWhat = taskWhat;
        this.url = url;
        this.file = file;
        this.start = start;
        this.current = current;
        this.end = end;
        this.sequence = sequence;
    }

    @Override
    public String toString() {
        return "Part{" +
                "state=" + state +
                ", taskWhat=" + taskWhat +
                ", url='" + url + '\'' +
                ", file='" + file + '\'' +
                ", start=" + start +
                ", current=" + current +
                ", end=" + end +
                ", sequence=" + sequence +
                '}';
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeInt(this.state);
        dest.writeLong(this.taskWhat);
        dest.writeString(this.url);
        dest.writeString(this.file);
        dest.writeLong(this.start);
        dest.writeLong(this.current);
        dest.writeLong(this.end);
        dest.writeInt(this.sequence);
    }

    protected Part(Parcel in) {
        this.id = in.readInt();
        this.state = in.readInt();
        this.taskWhat = in.readLong();
        this.url = in.readString();
        this.file = in.readString();
        this.start = in.readLong();
        this.current = in.readLong();
        this.end = in.readLong();
        this.sequence = in.readInt();
    }

    public static final Creator<Part> CREATOR = new Creator<Part>() {
        public Part createFromParcel(Parcel source) {
            return new Part(source);
        }

        public Part[] newArray(int size) {
            return new Part[size];
        }
    };
}